package com.mew.slots.core.common.quarze;

import com.mew.common.dc.redis.GameRoomRedisService;
import com.mew.common.entity.config.Game;
import com.mew.slots.core.common.handler.BetHandler;
import com.mew.slots.core.common.handler.InitializePlayHandlerMapping;
import com.zebra.common.TmsServerType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public class RollerQuarze {

    @Autowired
    private GameRoomRedisService gameRoomRedisService;

    @Value("${game.contains}")
    private int contains;
    @Value("${game.list}")
    private String gameList;

    @Scheduled(cron = "0 */6 * * * ?")//6s一次
    public void push(){

        List<String> vfxList = !"0".equals(gameList) ? new ArrayList<>(Arrays.asList(gameList.split(","))) : new ArrayList<>();

        List<Game> allGame = gameRoomRedisService.findAllGame().stream().filter(g->g.getGameType()== TmsServerType.Slots.code()).collect(Collectors.toList());
        if (contains == 1 && vfxList.size() > 0){
            for(String vfx:vfxList){
                Game game = allGame.stream().filter(gg -> vfx.equals(gg.getGameVfx())).findAny().orElse(null);
                if (game.getGameType()==1){
                    try {
                        BetHandler mapping = InitializePlayHandlerMapping.getMapping(game.getGameVfx());
                        if (null == mapping){
                            log.error("Roller Error " +game.getGameVfx()+ " is null");
                            continue;
                        }
                        mapping.readRoller();
                    } catch (Exception e) {
                    }
                }

            }
        }else {
            for (Game game : allGame) {
                if (vfxList.contains(game.getGameVfx())){
                    continue;
                }
                try {
                    BetHandler mapping = InitializePlayHandlerMapping.getMapping(game.getGameVfx());
                    if (null == mapping){
                        log.error("Roller Error " +game.getGameVfx()+ " is null");
                        continue;
                    }
                    mapping.readRoller();
                } catch (Exception e) {
                }
            }
        }


        log.info("roller success");
    }
}
